package net.wangliping.android.tools.taskmanager;

import java.util.ArrayList;
import java.util.List;

import net.wangliping.android.tools.R;
import net.wangliping.android.tools.util.ShellCommand;
import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;

public class TaskManager extends Activity implements OnClickListener {

	private final String LOG_TAG = "TastManager";

	private ListView mListView = null;
	private Button mButtonKillAll = null;
	private Button mButtonKillSelected = null;

	private TaskAdapter mTaskAdapter = null;
	
	private List<Programe> mProgList = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.taskmanager_main);

		mListView = (ListView) findViewById(R.id.task_list);
		mButtonKillAll = (Button) findViewById(R.id.btn_task_kill_all);
		mButtonKillSelected = (Button) findViewById(R.id.btn_task_kill_selected);
		
		mButtonKillAll.setOnClickListener(this);
		mButtonKillSelected.setOnClickListener(this);

		// List<PackageInfo> packages =
		// getPackageManager().getInstalledPackages(0);
		// mTaskAdapter = new TaskAdapter(this, packages);
		// mListView.setAdapter(mTaskAdapter);

		mProgList = getRunningProcess();
		mTaskAdapter = new TaskAdapter(this, mProgList);
		mListView.setAdapter(mTaskAdapter);
		
//		ShellCommand.isMachineRoot();
	}

	public List<Programe> getRunningProcess() {
		PackagesInfo pi = new PackagesInfo(this);

		ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
		List<RunningAppProcessInfo> run = am.getRunningAppProcesses();
		PackageManager pm = this.getPackageManager();
		List<Programe> list = new ArrayList<Programe>();

		for (RunningAppProcessInfo ra : run) {
			if (ra.processName.equals("system")
					|| ra.processName.equals("com.android.phone")) {
				continue;
			}

			Programe pr = new Programe();
			pr.setIcon(pi.getInfo(ra.processName).loadIcon(pm));
			pr.setName(pi.getInfo(ra.processName).loadLabel(pm).toString());
			pr.setPid(ra.pid);
			pr.setSystemApp(isSystemApp(ra.processName));
			list.add(pr);
		}
		return list;
	}

	public boolean isSystemApp(String pName) {
		try {
			PackageInfo pInfo = getPackageManager().getPackageInfo(pName, 0);

			if (((pInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0)
					|| ((pInfo.applicationInfo.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0))
				return true;
		} catch (NameNotFoundException e) {
//			e.printStackTrace();
		}
		
		return false;
	}

	@Override
	public void onClick(View v) {
		int id = v.getId();
		
		switch (id) {
		case R.id.btn_task_kill_all:
			for (Programe prog : mProgList)
				killProcess(prog.getName(), prog.getPid());
			break;
		case R.id.btn_task_kill_selected:
			for (Programe prog : mProgList)
				if (prog.isChecked())
					killProcess(prog.getName(), prog.getPid());
			break;
		default:
			break;
		}
		
		mProgList = getRunningProcess();
		mTaskAdapter = new TaskAdapter(this, mProgList);
		mListView.setAdapter(mTaskAdapter);		
	}
	
	private void killProcess(String pkgName, int pid) {
//		ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);   
//		manager.restartPackage(pkgName);
		
//		android.os.Process.killProcess(pid);
		
		String cmd = "kill -9 " + pid;
		ShellCommand.execRootCmd(cmd, null);
	}

}
